package com.itfreer.data.general.utils;

import com.itfreer.data.general.GlobalDataType;
import com.itfreer.utils.DateUtils;
import com.itfreer.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 类功能说明 定义值转换辅助类
 */
public class ValueConvertUtils {

	/**
	 * 转换数据类型
	 */
	public static Object ConvertValue(GlobalDataType globalDataType,
			Object fieldType, Object obj) {
		if (null == globalDataType) {
			return obj;
		}
		Object value = null;
		String strObj = null;
		switch (globalDataType) {
		case Boolean:
			boolean b = false;
			if (obj != null) {
				strObj = obj.toString();
				if (strObj.equals("是") || strObj.equals("1")) {
					b = true;
				} else if (strObj.equals("否") || strObj.equals("0")) {
					b = false;
				} else {
					try {
						b = Boolean.parseBoolean(strObj);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			value = b;
			break;
		case String:
		case Char:
		case Text:
			if (obj != null) {
				strObj = obj.toString();
				value = strObj;
			} else {
				value = null;
			}
			if (value == null) {
				value = "";
			}
			break;
		case Date:
			if(obj instanceof String){
				String dataString = obj.toString();
				if(dataString.startsWith("{")){
					String[] msgs = dataString.split(",");
					long time = -1;
					for(String item : msgs){
						String[] keyValue = item.split("=");
						if(keyValue[0].toLowerCase().trim().equals("time")){
							time = Long.parseLong(keyValue[1]);
							break;
						}
					}
					if(time!=-1){
						value = new Date(time);
					}else{
						value = obj;
					}
				}else{
					value = DateUtils.convertStringToDate(dataString);
				}
			} else if(obj instanceof Map){
				@SuppressWarnings("rawtypes")
				Map map = (Map)obj;
				Object time = map.get("time");
				value = new Date((long)time);
			} else {
				value = obj;
			}
			break;
		case DateTime:
			if(obj instanceof String){
				String dataString = obj.toString();
				if(dataString.startsWith("{")){
					String[] msgs = dataString.split(",");
					long time = -1;
					for(String item : msgs){
						String[] keyValue = item.split("=");
						if(keyValue[0].toLowerCase().trim().equals("time")){
							time = Long.parseLong(keyValue[1]);
							break;
						}
					}
					if(time!=-1){
						value = new Date(time);
					}else{
						value = obj;
					}
				}else{
					if(dataString.contains(":")){
						value = DateUtils.convertStringToDateTime(obj.toString());
					}else{
						value = DateUtils.convertStringToDate(obj.toString());
					}
				}
			} else if(obj instanceof Map){
				@SuppressWarnings("rawtypes")
				Map map = (Map)obj;
				Object time = map.get("time");
				value = new Date((long)time);
			} else {
				value = obj;
			}
			break;
		case Decimal:
			Float f = 0.0f;
			if (obj == null) {
				value = f;
			} else {
				strObj = obj.toString();
				if (!"".equals(strObj) && !"null".equals(strObj)
						&& !"Null".equals(strObj)) {
					try {
						f = new Float(strObj);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
				value = f;
			}
			break;
		case Double:
			Double dd = 0.0d;
			if (obj == null) {
				value = dd;
			} else {
				strObj = obj.toString();
				if (!"".equals(strObj) && !"null".equals(strObj)
						&& !"Null".equals(strObj)) {
					try {
						dd = new Double(strObj);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
				value = dd;
			}
			break;
		case Single:
		case Int16:
		case Int32:
		case UInt16:
		case UInt32:
		case UInt64:
			Integer i = 0;
			if (obj == null) {
				value = i;
			} else {
				strObj = obj.toString();
				if (!"".equals(strObj) && !"null".equals(strObj)
						&& !"Null".equals(strObj)) {
					try {
						i = new Integer(strObj);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
				value = i;
			}
			break;
		case Int64:
			Long l = 0l;
			if (obj == null) {
				value = l;
			} else {
				strObj = obj.toString();
				if (!"".equals(strObj) && !"null".equals(strObj)
						&& !"Null".equals(strObj)) {
					try {
						l = new Long(strObj);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
				value = l;
			}
			break;
		case ByteArray:
			if(obj instanceof String){
				try {
					value = ((String)obj).getBytes("UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
					value = ((String)obj).getBytes();
				}
			} else {
				value = obj;
			}
			break;
		default:
			value = strObj;
		}

		return value;
	}

	/**
	 * 转换数据源
	 */
	public static Object ConvertValue(Object obj) {
		Object nObject = ConvertValue(null, obj, obj);
		if (nObject == null)
			return null;
		else
			return nObject;
	}

	/**
	 * 转换为显示值
	 */
	public static String ConvertToCaption(Object dataType, Object value) {
		if (value == null)
			return "";

		if (dataType instanceof Boolean || dataType instanceof Character
				|| dataType instanceof String) {
			return "'" + value.toString() + "'";
		}
		if (dataType instanceof Date) {
			return "'" + value.toString() + "'";
		}

		if (dataType instanceof Byte || dataType instanceof Byte[]) {
			return "";
		}
		return value.toString();
	}

	/**
	 * 转换查询值 如：如果是字符类型，前后添加
	 */
	public static String ConvertSearchValue(Object dataType, String fieldName,
			Object value) {
		String strValue = "";
		if (dataType instanceof Boolean) {
			strValue = getStrSearchValue(fieldName, value);
		} else if (dataType instanceof Character) {
			strValue = getStrSearchValue(fieldName, value);
		} else if (dataType instanceof String) {
			strValue = getStrSearchValue(fieldName, value);
		} else if (dataType instanceof Date) {
			strValue = getStrSearchValue(fieldName, value);
		} else if (dataType instanceof BigDecimal) {
			strValue = getIntSearchValue(fieldName, value);
		} else if (dataType instanceof Double) {
			strValue = getIntSearchValue(fieldName, value);
		} else if (dataType instanceof Short) {
			strValue = getIntSearchValue(fieldName, value);
		} else if (dataType instanceof Integer) {
			strValue = getIntSearchValue(fieldName, value);
		}
		return strValue;
	}

	private static String getIntSearchValue(String fieldName, Object value) {
		String strValue = "";
		if (value == null)
			strValue = fieldName + " is null ";
		else if (StringUtils.IsNullOrSpace(value.toString()))
			strValue = "(" + fieldName + " is null or " + fieldName + " = 0 ) ";
		else
			strValue = fieldName + " = " + value.toString();
		return strValue;
	}

	private static String getStrSearchValue(String fieldName, Object value) {
		String strValue = "";
		if (value == null)
			strValue = fieldName + " is null ";
		else if (StringUtils.IsNullOrSpace(value.toString()))
			strValue = "(" + fieldName + " is null or " + fieldName
					+ " = '' ) ";
		else
			strValue = fieldName + " = '" + value.toString() + "' ";
		return strValue;
	}
}
